package com.erp.common.log;

import cn.hutool.core.util.IdUtil;
import com.erp.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;

import java.lang.reflect.Method;
@Aspect
@Slf4j
@Order(value = 1)
public class WebLogAspect {
    @Pointcut("execution(public * com.erp..controller.*.*(..))")
    public void controllerLog() {
    }

    @Around("controllerLog()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        String strClassName = point.getTarget().getClass().getName();
        String strMethodName = point.getSignature().getName();
        String uuid = IdUtil.simpleUUID();
        String commonLog = String.format("调用===>>>[类名]:%s,[方法]:%s,[uuid]:%s", strClassName, strMethodName, uuid);
        log.info(commonLog);

        Long startTime = System.currentTimeMillis();
        Object[] args = point.getArgs();
        Method method = ((MethodSignature) point.getSignature()).getMethod();

        if (null != args && args.length > 0) {
            String s = JsonUtils.objectToJson(args);
            log.info(commonLog + ",[参数]:{}", s);
        }
        Object obj = null;

        obj = point.proceed();

        if (null != obj) {
            String s = JsonUtils.objectToJson(obj);
            log.info(commonLog + ",[返回]:{}", s);
        }

        Long endTime = System.currentTimeMillis();
        log.info(commonLog + ",[耗时]:{}", endTime - startTime);
        return obj;
    }
}
